<?php

/***以下部分配置按需修改***/
//评论配置
$comment_config = [
    'table' => 'qe_comment',
    'reply_table' => 'qe_reply',
    'user_table' => 'core_user',
    'action_table' => 'core_action',
    'user_pk' => 'id',
    'nickname' => 'nickname',
    'avatar' => 'avatar',
    'cmt_default_status' => 1,
    'page_row' => 10,
    //评论完后触发什么事件
    'comment_callback' => 'app\\user\\service\\Callback::comment',
    //赞踩后触发的事件
    'action_callback' => 'app\\user\\service\\Callback::action',
    'allow_anonymous' => false, // 是否允许匿名评论 
];

//系统配置表
const CONFIG_TABLE = 'qe_config';
//上传文件表
const UPLOAD_FILES_TABLE = 'user_files';
//访问密钥
const ACCESS_KEY = 'admin688';
//文件CDN指向连接,按需要自定义配置
const CDN_URL = '';
//默认区域代码
const DEFAULT_AREA_CODE = '0';
//区域数据表
const AREA_DATA_TABLE = 'core_area_code';

const BLOCK_TABLE = 'qe_block';

//public访问目录
if (!defined('PUBLIC_PATH'))
    define('PUBLIC_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
//上传目录
if (!defined('UPLOAD_PATH'))
    define('UPLOAD_PATH', PUBLIC_PATH . 'uploads' . DIRECTORY_SEPARATOR);
!is_dir(UPLOAD_PATH) && mkdir(UPLOAD_PATH, 0777, true);

$config_file = dirname(__DIR__, 2) . '/apps/config.php';
//数据库配置
global $db_config, $qe_db_config;

if (!$db_config && is_file($config_file)) {
    define('RUNTIME_PATH', dirname(__DIR__, 2) . '/server/runtime/');
    define('APP_DEBUG', true);
    $conf = include_once $config_file;
    $db_config = $conf['database']['connections']['mysql'];
}

$qe_db_config = $qe_db_config ?: array(
    'host' => $db_config['hostname'] ?? 'localhost',
    'username' => $db_config['username'] ?? 'root',
    'password' => $db_config['password'] ?? 'root',
    'db' => $db_config['database'] ?? 'xnc',
    'port' => 3306,
    'prefix' => $db_config['prefix'] ?? '',
    'charset' => $db_config['charset'] ?? 'utf8'
);

//不同的系统需要修改获取用户id的逻辑
function get_user_id()
{
    $user_info = get_user_info();
    return $user_info['id'] ?? 0;
}

function get_user_info()
{
    $data = [];
    global $comment_config;
    // 获取用户标识
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $ip = _ip_();
    $anms_id = isset($_COOKIE['qcmt_anms_id']) ? $_COOKIE['qcmt_anms_id'] : '';

    // 如果没有匿名ID,则生成一个
    if (!$anms_id) {
        $anms_id = 'anms_' . md5($user_agent . $ip . time());
    }

    // 获取保存的匿名信息
    $nickname = isset($_COOKIE['qcmt_anms_nickname']) ? $_COOKIE['qcmt_anms_nickname'] : '';
    $avatar = isset($_COOKIE['qcmt_anms_avatar']) ? $_COOKIE['qcmt_anms_avatar'] : '';

    // 如果是匿名用户
    if ($comment_config['allow_anonymous'] && strpos($anms_id, 'anms_') === 0) {
        $data = [
            'id' => $anms_id,  // 使用匿名ID作为用户标识
            'nickname' => $nickname,
            'avatar' => $avatar,
            'is_anms' => true
        ];
        // 设置cookie保持匿名身份
        setcookie('qcmt_anms_id', $anms_id, time() + 86400 * 30, '/');
        if ($nickname) {
            setcookie('qcmt_anms_nickname', $nickname, time() + 86400 * 30, '/');
        }
        if ($avatar) {
            setcookie('qcmt_anms_avatar', $avatar, time() + 86400 * 30, '/');
        }
    } else {
        $current_user_id = $_SERVER['HTTP_X_CURRENT_USER_ID'] ?? '';
        if ($current_user_id) {
            $user_info = db()->where('id', $current_user_id)->getOne($comment_config['user_table']);
            if($user_info) {
                $data = [
                    'id' => $user_info['id'],
                    'nickname' => $user_info['nickname'],
                    'avatar' => $user_info['avatar'],
                    'is_anms' => false
                ];
            }
        }
    }

    return $data;
}

function need_login()
{
    header("location:/user");
}

function is_admin()
{
    $user = get_user_info();
    return $user && $user['user'] && isset($user['user']['is_admin']) && $user['user']['is_admin'] ? true : false;
}


function s($name = '', $value = '')
{
    if (function_exists('data')) {
        return  data($name, $value, 'SESSION');
    }
    //对session增删改操作
    if (is_array($name)) {
        foreach ($name as $k => $v) {
            $_SESSION[$k] = $v;
        }
        return true;
    } elseif ($value === '') {
        return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
    } elseif ($value === null) {
        unset($_SESSION[$name]);
        return null;
    } else {
        $_SESSION[$name] = $value;
        return true;
    }
}
/***以上部分配置按需修改***/
